/** 
 * 电视剧视频链接控制器模块
 */
module controller.TeleplayVlinkController;

import std.conv : to;

import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.data.json : Json;

import model.TeleplayVlink;
import repository.TeleplayLinksRepo;
import utils.RespData : RespData;
import utils.ObjMapping : ObjMapping;
import utils.SecurityVerification : signValidator;

TeleplayLinksRepo _tlRepo;

static this ()
{
    _tlRepo = new TeleplayLinksRepo();
}

static ~this ()
{
    destroy(_tlRepo);
}

/** 
* 获取一个电视剧视频链接
*/
void getOneTeleplayLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    string idStr = req.params.get("id", "");
    if (idStr == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"));
        return;
    }
    uint id = to!uint(idStr);

    auto tlink = _tlRepo.getOneTeleplayLink(id);
    if (tlink is null)
    {
        resp.writeJsonBody(RespData.newData(404, "未找到"), 404);
        return;
    }

    auto respData = RespData.newData!(TeleplayVlinkDto)(200, "ok", ObjMapping.mappingToObject!(
            TeleplayVlinkDto, TeleplayVlink)(tlink));
    resp.writeJsonBody(respData);
}

/** 
* 添加电视剧视频链接
*/
void addTeleplayLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    Json json = req.json;
    string teleplay_id = json["teleplay_id"].to!string();
    string name = json["name"].to!string();
    string url = json["url"].to!string();
    string real_url = json["real_url"].to!string();

    string sign = json["sign"].to!string();

    import std.format : format;

    string reqParamsStr = format("%s%s", name, url);
    if (!signValidator(reqParamsStr, sign))
    {
        resp.writeJsonBody(RespData.newData(403, "签名不正确！"), 403);
        return;
    }

    TeleplayVlinkDtoFromCreate tl = new TeleplayVlinkDtoFromCreate(teleplay_id, name, url, real_url);
    bool res = _tlRepo.addTeleplayLink(
        ObjMapping.mappingToObject!(TeleplayVlink, TeleplayVlinkDtoFromCreate)(
            tl));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "error"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"), 201);
}

/** 
* 删除一个电视剧视频链接
*/
void removeTeleplayLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    string idStr = req.params.get("id", "");
    string sign = req.query.get("sign", "");
    if (idStr == "" || sign == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"));
        return;
    }
    uint id = to!uint(idStr);

    if (!signValidator(idStr, sign))
    {
        resp.writeJsonBody(RespData.newData(403, "签名不正确！"), 403);
        return;
    }

    bool res = _tlRepo.removeTeleplayLink(id);
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "error"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"));
}

/** 
* 编辑电视剧视频链接
*/
void editTeleplayLink (HTTPServerRequest req, HTTPServerResponse resp)
{
    string idStr = req.params.get("id", "");
    if (idStr == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"));
        return;
    }
    uint id = to!uint(idStr);

    Json json = req.json;
    string name = json["name"].to!string();
    string url = json["url"].to!string();
    string real_url = json["real_url"].to!string();

    string sign = json["sign"].to!string();

    import std.format : format;

    string reqParamsStr = format("%s%s", name, url);
    if (!signValidator(reqParamsStr, sign))
    {
        resp.writeJsonBody(RespData.newData(403, "签名不正确！"), 403);
        return;
    }

    TeleplayVlinkDtoFromUpdate tl = new TeleplayVlinkDtoFromUpdate(name, url, real_url);
    bool res = _tlRepo.editTeleplayLink(id, ObjMapping.mappingToObject!(
            TeleplayVlink, TeleplayVlinkDtoFromUpdate)(tl));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "error"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"));
}
